package cn.hunau.bookstore.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.hunau.bookstore.dao.BookDao;
import cn.hunau.bookstore.exception.DaoException;
import cn.hunau.bookstore.pojo.Book;
import cn.hunau.bookstore.utils.ConnectionUtils;
import cn.hunau.bookstore.utils.RowMapper;

/**
 * @author HuangBo
 *
 *         2022-01-22 11:20:41
 *
 */
public class BookDaoImpl implements BookDao {

	@Override
	public List<Book> queryBookByPageOrderByPubDate(int beginIndex, int offset) throws DaoException {
		final String sql = "SELECT t.id,title,author,publisher,price,FORMAT,paper,packing,isbn,pic,cid,isonline,pubdate,content "
				+ "FROM t_book t,t_book_content b WHERE t.id=b.bid and content is not null ORDER BY pubdate DESC LIMIT ?,?";

		return ConnectionUtils.query(sql, new RowMapper<List<Book>>() {
			@Override
			public List<Book> rowMapper(ResultSet rs) throws Exception {
				List<Book> books = new ArrayList<>();
				while (rs.next()) {
 					books.add(parseResultSet(rs));
				}

				return books;
			}
		}, beginIndex, offset);

	}

	private Book parseResultSet(ResultSet rs) throws Exception {
		Book b = new Book();
		b.setId(rs.getInt("id"));
		b.setTitle(rs.getString("title"));
		b.setAuthor(rs.getString("author"));
		b.setPublisher(rs.getString("publisher"));
		b.setPrice(rs.getDouble("price"));
		b.setFormat(rs.getString("format"));
		b.setPaper(rs.getString("paper"));
		b.setPacking(rs.getString("packing"));
		b.setIsbn(rs.getString("isbn"));
		b.setPic(rs.getString("pic"));
		b.setCid(rs.getInt("cid"));
		b.setIsonline(rs.getBoolean("isonline"));
		b.setPubdate(rs.getDate("pubdate").toLocalDate());
		b.setContent(rs.getString("content"));
		return b;
	}

	@Override
	public int count() throws DaoException {
		final String sql = "select count(*) from t_book";

		return ConnectionUtils.query(sql, new RowMapper<Integer>() {
			@Override
			public Integer rowMapper(ResultSet rs) throws Exception {
				Integer i = 0;
				if (rs.next()) {
					i = rs.getInt(1);
				}
				return i;
			}

		});

	}

	@Override
	public List<Book> queryNewIndexBook(int beginIndex,int offset) throws DaoException {
		final String sql = "select id,bid,title,price,author,pic,`order` FROM t_index_book where tag=0 limit ?,? " ;
		return ConnectionUtils.query(sql, new RowMapper<List<Book>>() {
			@Override
			public List<Book> rowMapper(ResultSet rs) throws Exception {
				List<Book> books = new ArrayList<>();
				while (rs.next()) {
					Book b = new Book();
					b.setId(rs.getInt("bid"));
					b.setTitle(rs.getString("title"));
					b.setAuthor(rs.getString("author")); 
					b.setPrice(rs.getDouble("price")); 
					b.setPic(rs.getString("pic"));
					books.add(b);
				}

				return books;
			}
		}, beginIndex, offset);
	}

	@Override
	public void insertIndexBook(Book book,Integer tag) throws DaoException {
		final String sql="insert t_index_book(bid,title,price,author,pic,`order`,tag,active) values(?,?,?,?,?,?,?,?)";
		Object [] args= {book.getId(),book.getTitle(),book.getPrice(),book.getAuthor(),book.getPic(),book.getOrder(),tag,1};
 		ConnectionUtils.update(sql, args);
	}

}
